package com.ilxqx.framework.schedule.util;

import com.ilxqx.framework.schedule.constant.JobDetailConstants;
import com.ilxqx.framework.schedule.constant.TriggerConstants;
import com.ilxqx.framework.system.upload.enumeration.FileTypeEnum;
import com.ilxqx.framework.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

import java.time.LocalDateTime;

/**
 * 触发器工具类
 * @author venus
 */
@Slf4j
public final class TriggerUtils {

    /**
     * 获取测试任务触发器
     * @param id 任务ID数据
     * @return 触发器对象
     */
    public static Trigger createTestJobTrigger(String id) {
        return TriggerBuilder.newTrigger()
                .withIdentity(id, TriggerConstants.DEFAULT_GROUP)
                .usingJobData("id", id)
                .forJob(JobDetailConstants.TEST_JOB_KEY)
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(3))
                .startNow()
                .build();
    }

    /**
     * 获取测试任务触发器key
     * @param id 任务ID数据
     * @return 触发器key
     */
    public static TriggerKey getTestJobTriggerKey(String id) {
        return TriggerKey.triggerKey(id, TriggerConstants.DEFAULT_GROUP);
    }

    /**
     * 创建文件删除Job触发器
     * @param key 文件唯一标识
     * @param fileTypeEnum 文件类型枚举
     * @return 触发器
     */
    public static Trigger createFileDeletionJobTrigger(String key, FileTypeEnum fileTypeEnum) {
        return TriggerBuilder.newTrigger()
            .withIdentity(key, TriggerConstants.FILE_DELETION_GROUP)
            .usingJobData("key", key)
            .usingJobData("fileType", fileTypeEnum.name())
            .forJob(JobDetailConstants.FILE_DELETION_KEY)
            .startAt(DateUtils.localDateTimeToDate(LocalDateTime.now().plusMinutes(60)))
            .build();
    }

    /**
     * 获取文件删除的触发器Key
     * @param key 文件唯一标识
     * @return 触发器Key
     */
    public static TriggerKey getFileDeletionJobTrigger(String key) {
        return TriggerKey.triggerKey(key, TriggerConstants.FILE_DELETION_GROUP);
    }

}
